import {useEffect} from 'react'
import { useDispatch, useSelector } from "react-redux";
import { loginout } from "../store/reducer/authSlice";

const useAuthLoginout = () => {
    const dispatch = useDispatch()
    const auth = useSelector(state => state.auth)
    useEffect(() => {
      const timeout = auth.expirationTime - Date.now()
      if (timeout < 6000) {
        dispatch(loginout())
        return
      }
      const timer = setTimeout(() => {
        dispatch(loginout())
      }, timeout);
      return () => {
        clearTimeout(timer)
      };
    }, [auth]);
}

export default useAuthLoginout